﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    public class ManualEntryController : Controller
    {
        private ManualEntryInfo maninfo = new ManualEntryInfo(true);
        public ActionResult ManualEntryIndex()
        {
            ViewData["MacID"] = GetMacID(true);
            ViewData["PeriodCode"] = GetPeriodCode(true);
            ViewData["DepositTypeID"] = GetDepositTypeID(true);
            return View();
        }

        /// <summary>
        /// 获取食堂名称列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetMacID(bool bo)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = maninfo.GetMacID();
            if (bo == true)
            {
                dic.Add(-1, "全部");
            }
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["MacID"]), ds.Tables[0].Rows[i]["Nodename"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取餐次(用于绑定下拉框)
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetPeriodCode(bool bo)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = maninfo.GetPeriodCode();
            if (bo == true)
            {
                dic.Add(-1, "全部");
            }
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["PeriodCode"]), ds.Tables[0].Rows[i]["PeriodName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取名称(用于绑定下拉框)
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetDepositTypeID(bool bo)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = maninfo.GetDepositTypeID();
            if (bo == true)
            {
                dic.Add(-1, "全部");
            }
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["DepositTypeID"]), ds.Tables[0].Rows[i]["DepositName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取手工录入列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetManualEntryList(PageVM pvm)
        {
            string query = " where 1=1 ";
            if (string.IsNullOrEmpty(pvm.query))
            {
                query += " and 1=1";
            }
            else
            {
                string[] param = pvm.query.Split(',');
                string StartTime = param[0];
                string EndTime = param[1];
                string MacID = param[2];
                string PeriodCode = param[3];
                string DepositTypeID = param[4];
                query += " and RDATE>='" + StartTime + "' and RDATE<='" + EndTime + "'";
                if (MacID != "-1")
                {
                    query += " and MACNO='" + MacID + "'";
                }
                if (PeriodCode != "-1")
                {
                    query += " and PERIOD_CODE='" + PeriodCode + "'";
                }
                if (DepositTypeID != "-1")
                {
                    query += " and DEALMARK='" + DepositTypeID + "'";
                }
            }
            IList<ManualEntryInfo> manlist = maninfo.SelManualEntryInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = maninfo.GetManualEntryCount(query);
            List<string> v;
            foreach (ManualEntryInfo item in manlist)
            {
                #region
                v = new List<string>();
                v.Add(item.ID.ToString());
                v.Add(item.RDATE.ToString());
                v.Add(item.NODENAME);
                v.Add(item.PERIOD_NAME);
                v.Add(item.DEPOSITNAME);
                v.Add(item.Price.ToString());
                v.Add(item.Count.ToString());
                v.Add(item.ConMoney.ToString());
                v.Add(item.OperatorID);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.ID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public ActionResult ManualEntryAdd()
        {
            ViewData["MenuType"] = GetMenuType();
            ViewData["Menuleft"] = SelLeftBox("");
            ViewData["Menuright"] = SelRightBox();

            ViewData["MacID"] = GetMacID(false);
            ViewData["PeriodCode"] = GetPeriodCode(false);
            ViewData["DepositTypeID"] = GetDepositTypeID(false);
            return View();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        public ActionResult ManualEntryEdit(string id)
        {
            ViewData["MenuType"] = GetMenuType();
            ViewData["Menuleft"] = SelLeftBox("");
            ViewData["Menuright"] = SelRightBox(id);
            ManualEntryVM vm = new ManualEntryVM();
            ManualEntryInfo info = new ManualEntryInfo();

            maninfo = info.GetManualEntryInfoOne(id);
            vm.ID = maninfo.ID;
            vm.DateEdit = maninfo.RDATE.ToString("yyyy-MM-dd");
            vm.NODENAME = maninfo.NODENAME;
            vm.PERIOD_NAME = maninfo.PERIOD_NAME;
            vm.DEPOSITNAME = maninfo.DEPOSITNAME;
            vm.Count = maninfo.Count.ToString();
            return View(vm);
        }

        /// <summary>
        /// 获取下拉列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> SelLeftBox(string Select)
        {
            string query = " and 1=1 ";
            if (string.IsNullOrEmpty(Select))
            {
                query += " and 1=1";
            }
            else
            {
                string param = Select;
                if (param.ToString() != "-1")
                {
                    query += " and MenuType='" + param + "'";
                }
            }
            IList<ManualEntryInfo> menlist = maninfo.SelLeftBox(query);
            IDictionary<int, string> dic = new Dictionary<int, string>();
            foreach (ManualEntryInfo item in menlist)
            {
                dic.Add(Convert.ToInt32(item.MenuId), item.MenuName);
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public IList<SelectListItem> SelRightBox()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public IList<SelectListItem> SelRightBox(string id)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = maninfo.GetCon_Menus(id);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                dic.Add(Convert.ToInt32(dr["MenuId"]), dr["MenuName"].ToString());
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public ActionResult GetLeftBox(string Select)
        {
            ViewData["Menuleft"] = SelLeftBox(Select);
            return Json(ViewData["Menuleft"]);
        }

        /// <summary>
        /// 获取菜单类型列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetMenuType()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(-1, "请选择...");
            dic.Add(1, "大荤");
            dic.Add(2, "小荤");
            dic.Add(3, "素菜");
            dic.Add(4, "汤类");
            dic.Add(5, "饭类");
            dic.Add(6, "点心");
            dic.Add(7, "早餐");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 保存（新增界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveManualEntryAdd(ManualEntryVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                string menall = model.MenuAll.Remove(model.MenuAll.Length - 1);
                ManualEntryInfo man = new ManualEntryInfo()
                {
                    MenuAll = menall,
                    RDATE = Convert.ToDateTime(model.DateEdit),
                    MacID = model.MacID,
                    PeriodCode = model.PeriodCode,
                    DepositTypeID = model.DepositTypeID,
                    Count = Convert.ToInt32(model.Count),
                    OperatorID = ((LogOnModel)(System.Web.HttpContext.Current.Session["user"])).LogName
                };
                result = maninfo.SaveManualEntryAdd(man);
            }
            return Json(result);
        }

        /// <summary>
        /// 保存（修改界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveManualEntryEdit(ManualEntryVM model)
        {
            int result = 0;
            //model.DateEdit = DateTime.Now.ToString("yyyy-MM-dd");
            //model.DEPOSITNAME = "";
            //model.leftbox = "";
            //model.NODENAME = "";
            //model.PERIOD_NAME = "";
            //model.rightbox = "";
            //if (ModelState.IsValid)
            //{
            string menall = model.MenuAll.Remove(model.MenuAll.Length - 1);
            ManualEntryInfo man = new ManualEntryInfo()
            {
                ID = model.ID,
                Count = Convert.ToInt32(model.Count),
                MenuAll = menall
            };
            result = maninfo.SaveManualEntryEdit(man);
            //}
            return Json(result);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ActionResult DeleteManualEntry(string ID)
        {
            int result = 0;
            result = maninfo.DeleteManualEntry(ID);
            return Json(result);
        }

        /// <summary>
        /// 日期不能为空验证
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckDateNull(string DateEdit)
        {
            bool bo = DateEdit == "" ? false : true;
            return Json(bo);
        }

        /// <summary>
        /// 数量不能为空验证
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckCountNull(string Count)
        {
            bool bo = Count == "" ? false : true;
            return Json(bo);
        }
    }
}
